#ifndef _MAP_C
#define _MAP_C

#include "map.h"

//Functions
//functions that run the map movement and or location
//Init map

int door_counter=0;

void init_tiles(void){
    int i;
    for(i=0;i<46;i++){
        tile[i]=create_bitmap(TILE_SIZE,TILE_SIZE);
        masked_blit((BITMAP *)maptiles[i].dat,tile[i],0,0,0,0,TILE_SIZE,TILE_SIZE);
    }
}    

//draw map to secondary buffer
void draw_map(void){
    short i,j;
    for(i=0;i<25;i++){
             for(j=0;j<40;j++){
                  masked_blit(tile[tiles[i][j].bmp],buffer,0,0,tiles[i][j].x1,tiles[i][j].y1,TILE_SIZE,TILE_SIZE);
             }
    }       
}

void destroy_tiles(void){
    int i;
    for(i=0;i<46;i++){
        destroy_bitmap(tile[i]);
    }
}    

int open_doors(void){
   int i,j=0;
   if(door_counter<10)door_counter++;
   if(door_counter==10){
    for(i=0;i<25;i++){
        if(tiles[i][0].bmp==43||tiles[i][0].bmp==44){
         tiles[i][0].att=0;
         tiles[i][0].bmp=45;
         j++;
         break;
        }    
    }
    for(i=25;i>0;i--){
        if(tiles[i][39].bmp==43||tiles[i][39].bmp==44){
         tiles[i][39].att=0;
         tiles[i][39].bmp=45;
         j++;
         break;
        }    
    }
    for(i=0;i<40;i++){
        if(tiles[24][i].bmp==44||tiles[24][i].bmp==43){
         tiles[24][i].att=0;
         tiles[24][i].bmp=45;
         j++;
         break;
        }    
    }
    for(i=40;i>0;i--){
        if(tiles[0][i].bmp==44||tiles[0][i].bmp==43){
         tiles[0][i].att=0;
         tiles[0][i].bmp=45;
         j++;
         break;
        }    
    }
    door_counter=0;
   }  
   return j;
}

int close_doors(void){
   int i,j=0;
   if(door_counter<10)door_counter++;
   if(door_counter==10){
    for(i=0;i<25;i++){
        if(tiles[i][0].bmp==45){
         tiles[i][0].att=1;
         tiles[i][0].bmp=43;
         j++;
         break;
        }    
    }
    for(i=25;i>0;i--){
        if(tiles[i][39].bmp==45){
         tiles[i][39].att=1;
         tiles[i][39].bmp=43;
         j++;
         break;
        }    
    }
    for(i=0;i<40;i++){
        if(tiles[24][i].bmp==45){
         tiles[24][i].att=1;
         tiles[24][i].bmp=44;
         j++;
         break;
        }    
    }
    for(i=40;i>0;i--){
        if(tiles[0][i].bmp==45){
         tiles[0][i].att=1;
         tiles[0][i].bmp=44;
         j++;
         break;
        }    
    }
    door_counter=0;
   }
   return j;
}

int save_map(void){
    int i,j;
    PACKFILE *pfile;
    pfile= pack_fopen("level.dat","wp");
    if(!pfile)return -1;
    for(i=0;i<MAP_H;i++){
        for(j=0;j<MAP_W;j++){
            pack_iputl(tiles[i][j].bmp, pfile);
            pack_iputl(tiles[i][j].x1, pfile);
            pack_iputl(tiles[i][j].y1, pfile);
            pack_iputl(tiles[i][j].x2, pfile);
            pack_iputl(tiles[i][j].y2, pfile);
            pack_iputl(tiles[i][j].att, pfile);
            pack_iputl(tiles[i][j].mov, pfile);
            pack_iputl(tiles[i][j].energy, pfile);
        }
    }
    pack_fclose(pfile);
    return 0;
}            

int load_map(char map_name[24]){
    int i,j;
    PACKFILE *pfile;
    pfile= pack_fopen(map_name,"rp");
    if(!pfile)return -1;
    for(i=0;i<MAP_H;i++){
        for(j=0;j<MAP_W;j++){
            tiles[i][j].bmp=pack_igetl(pfile);
            tiles[i][j].x1=pack_igetl(pfile);
            tiles[i][j].y1=pack_igetl(pfile);
            tiles[i][j].x2=pack_igetl(pfile);
            tiles[i][j].y2=pack_igetl(pfile);
            tiles[i][j].att=pack_igetl(pfile);
            tiles[i][j].mov=pack_igetl(pfile);
            tiles[i][j].energy=pack_igetl(pfile);
        }
    }
    pack_fclose(pfile);
    return 0;
}            

int load_test_map(BITMAP *back){
    int i,j,ch,done=0,no_load=0;
    char temp_name[5]={0,0,0,0,0},
        char_buf[2]={0,0};
     
    BITMAP *buff;
    buff=create_bitmap(SCREEN_SIZE_W,SCREEN_SIZE_H);
    clear_keybuf();
    while(!done){
         blit(back,buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
         textprintf_centre_ex(buff,font,320,250,makecol(rand()%255,rand()%255,rand()%255),-1,"TYPE THE NAME OF THE MAP AND THEN ENTER (IE 'level1.dat')");
         textprintf_centre_ex(buff,font,320,300,makecol(255,255,255),-1,"%s",(char *)temp_name);
         draw_buffer(buff,0,0,0,0,SCREEN_SIZE_W,SCREEN_SIZE_H);
         ch = readkey();          
         if((ch&0xff)>='A'&&(ch&0xff)<='Z'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }
         else if((ch&0xff)>='a'&&(ch&0xff)<='z'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }    
         else if((ch&0xff)>='0'&&(ch&0xff)<='9'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }  
         else if((ch&0xff)=='.'){
             sprintf(char_buf,"%c",(ch&0xff));
             strcat(temp_name,char_buf);
         }      
         else if((ch&0xff)==27){
             no_load=1;done=1;
         } 
         else if((ch&0xff)==8){
             temp_name[strlen(temp_name)-1]=0;
         } 
         else if((ch&0xff)==13){
             done=1;
         }    
    }  
    destroy_bitmap(buff);  
    if(no_load)return 0;
    else{ 
     char map_name[25] = "maps/";
     strcat(map_name,temp_name);
     PACKFILE *pfile;
     pfile= pack_fopen(map_name,"rp");
     if(!pfile){
         return 0;
     }    
     for(i=0;i<MAP_H;i++){
         for(j=0;j<MAP_W;j++){
             tiles[i][j].bmp=pack_igetl(pfile);
             tiles[i][j].x1=pack_igetl(pfile);
             tiles[i][j].y1=pack_igetl(pfile);
             tiles[i][j].x2=pack_igetl(pfile);
             tiles[i][j].y2=pack_igetl(pfile);
             tiles[i][j].att=pack_igetl(pfile);
             tiles[i][j].mov=pack_igetl(pfile);
             tiles[i][j].energy=pack_igetl(pfile);
         }
     }
     pack_fclose(pfile);
     return 1;
    }     
}

char *get_map_number(int num){
    char *map_num="maps/classic1.dat";
    switch (num){
        case 1:
            map_num="maps/level1.dat";
            break;
        case 2:
            map_num="maps/level2.dat";
            break;
        case 3:
            map_num="maps/level3.dat";
            break;
        case 4:
            map_num="maps/level4.dat";
            break;
        case 5:
            map_num="maps/level5.dat";
            break;
        case 6:
            map_num="maps/level6.dat";
            break;
        case 7:
            map_num="maps/level7.dat";
            break;
        case 8:
            map_num="maps/level8.dat";
            break;
        case 9:
            map_num="maps/level9.dat";
            break;
        case 10:
            map_num="maps/level10.dat";
            break;
        case 11:
            map_num="maps/level11.dat";
            break;
        case 12:
            map_num="maps/level12.dat";
            break;
        case 13:
            map_num="maps/level13.dat";
            break;
        case 14:
            map_num="maps/level14.dat";
            break;
        case 15:
            map_num="maps/level15.dat";
            break;
        case 16:
            map_num="maps/level16.dat";
            break;
        case 17:
            map_num="maps/level17.dat";
            break;
        case 18:
            map_num="maps/level18.dat";
            break;
        case 19:
            map_num="maps/level19.dat";
            break;
        case 20:
            map_num="maps/level20.dat";
            break;
        case 21:
            map_num="maps/classic1.dat";
            break;
        case 22:
            map_num="maps/classic2.dat";
            break;
        case 23:
            map_num="maps/classic3.dat";
            break;
        case 24:
            map_num="maps/classic4.dat";
            break;
        case 25:
            map_num="maps/classic5.dat";
            break;
        case 26:
            map_num="maps/classic6.dat";
            break;
        case 27:
            map_num="maps/classic7.dat";
            break;
        case 28:
            map_num="maps/classic8.dat";
            break;
        case 29:
            map_num="maps/classic9.dat";
            break;
        case 30:
            map_num="maps/classic10.dat";
            break;
    }
    return map_num;
}        

#endif
